package cn.yrec.rain.analysis.exception;


import cn.yrec.rain.analysis.domain.ResponseData;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.Serializable;

/**
 * 自定义全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler implements Serializable {

    /**
     * 方法描述: 请求参数异常处理
     * @author yanglichen
     * @date 2023-03-30 23:26
     * @param msg 错误信息
     * @return 错误信息
     **/
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(RequestParamException.class)
    public ResponseData requestParamException(Exception msg) {
        return ResponseData.fail(HttpStatus.BAD_REQUEST, msg.getMessage());
    }

    /**
     * 方法描述: 请求参数格式异常
     * @author yanglichen
     * @date 2023-03-30 23:26
     * @param msg 错误信息
     * @return 错误信息
     **/
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(RequestDataFormatException.class)
    public ResponseData requestDataFormatException(Exception msg) {
        return ResponseData.fail(HttpStatus.BAD_REQUEST, msg.getMessage());
    }

    /**
     * 方法描述: 请求参数格式异常
     * @author yanglichen
     * @date 2023-03-30 23:26
     * @param msg 错误信息
     * @return 错误信息
     **/
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ModelDataException.class)
    public ResponseData modelDataException(Exception msg) {
        return ResponseData.fail(HttpStatus.INTERNAL_SERVER_ERROR, msg.getMessage());
    }


}
